//
//  BTEUserContactTableViewCell.swift
//  BTE
//
//  Created by 张文 on 2025/9/2.
//

import UIKit

class BTEUserContactTableViewCell: UITableViewCell {
    
    var model: BTECarnepalaeoglafewfwcioewelogyNetModel? {
        didSet {
            guard let m = model else {
                return
            }
            bttit.text = m.aftermath
            b1.text = m.cauf
            t1text.placeholder = m.vain
            b2.text = m.aeromechanics
            t2text.placeholder = m.concludent
            
            // 关系
            if let f = m.panderism?.filter({$0.choised == true}).first {
                t1text.text = f.manificum
            }
            
            if let bg = m.manificum,bg.count > 0{
                t2text.text = "\(bg)-"
            }
            
            if let be = m.meedful,be.count > 0 {
                t2text.text?.append(be)
            }
        }
    }
    
    var action:(()->())?
    
    var choiseUser:(()->())?
    
    let bimageview = UIImageView.init(image: UIImage.init(named: "fewfwef333333"))
    let bttit = UILabel()
    
    //select
    let box1 = UIView()
    let b1 = UILabel()
    let t1text = UITextField()
    let imagev = UIImageView.init(image: UIImage.init(named: "Vector"))
    
    //user
    let box2 = UIView()
    let b2 = UILabel()
    let t2text = UITextField()
    let imagev2 = UIImageView.init(image: UIImage.init(named: "Vector"))

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        contentView.addSubview(bimageview)
        contentView.addSubview(bttit)
        bimageview.snp.makeConstraints { make in
            make.width.height.equalTo(28)
            make.left.equalToSuperview().offset(19)
            make.top.equalToSuperview().offset(15)
        }
        
        bttit.textColor = .black
        bttit.font = UIFont.systemFont(ofSize: 18, weight: .bold)
        bttit.snp.makeConstraints { make in
            make.centerY.equalTo(bimageview)
            make.left.equalTo(bimageview.snp.right).offset(12)
        }
     
        box1.backgroundColor = .white
        box1.roundCorners(.allCorners, radius: 12)
        box1.layer.borderWidth = 3
        box1.layer.borderColor = UIColor.colorWithHex("#082252")!.cgColor
        
        contentView.addSubview(box1)
        box1.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(19)
            make.top.equalTo(bimageview.snp.bottom).offset(12)
        }
        
        box1.addSubview(b1)
        b1.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(14)
            make.left.equalToSuperview().offset(16)
        }
        
        t1text.isUserInteractionEnabled = false
        t1text.font = .systemFont(ofSize: 14,weight: .bold)
        t1text.textColor = .black
        t1text.isUserInteractionEnabled = false
        box1.addSubview(t1text)
        t1text.snp.makeConstraints { make in
            make.left.equalTo(b1)
            make.right.equalToSuperview().offset(-32)
            make.height.equalTo(44)
            make.bottom.equalToSuperview()
            make.top.equalTo(b1.snp.bottom).offset(10)
        }
        
        box1.addSubview(imagev)
        imagev.snp.makeConstraints { make in
            make.width.equalTo(7)
            make.height.equalTo(14)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-16)
        }
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(displayad))
        box1.addGestureRecognizer(tap)
        box1.isUserInteractionEnabled = true
        
        box2.backgroundColor = .white
        box2.roundCorners(.allCorners, radius: 12)
        box2.layer.borderWidth = 3
        box2.layer.borderColor = UIColor.colorWithHex("#082252")!.cgColor
        
        contentView.addSubview(box2)
        box2.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(19)
            make.top.equalTo(box1.snp.bottom).offset(12)
            make.bottom.equalToSuperview().offset(-12)
        }
        
        box2.addSubview(b2)
        b2.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(14)
            make.left.equalToSuperview().offset(16)
        }
        
        t2text.isUserInteractionEnabled = false
        t2text.font = .systemFont(ofSize: 14,weight: .bold)
        t2text.textColor = .black
        t2text.isUserInteractionEnabled = false
        box2.addSubview(t2text)
        t2text.snp.makeConstraints { make in
            make.left.equalTo(b2)
            make.right.equalToSuperview().offset(-32)
            make.height.equalTo(44)
            make.bottom.equalToSuperview()
            make.top.equalTo(b2.snp.bottom).offset(10)
        }
        
        box2.addSubview(imagev2)
        imagev2.snp.makeConstraints { make in
            make.width.equalTo(7)
            make.height.equalTo(14)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-16)
        }
        
        let tap2 = UITapGestureRecognizer.init(target: self, action: #selector(displayadc))
        box2.addGestureRecognizer(tap2)
        box2.isUserInteractionEnabled = true
        
    }
    
    @objc func displayad(){
        if let topViewController = getTopViewController() {
            let vc = BTESelectAlertViewController.init()
            vc.m = model?.panderism
            vc.modalPresentationStyle = .overFullScreen
            topViewController.present(vc, animated: false, completion: nil)
            
            vc.action = {[weak self] in
                self?.model?.anhydridization = self?.model?.panderism?.filter({$0.choised == true}).first?.dikegrave
                self?.action?()
                vc.dismiss(animated: false)
            }
            
        }
    }
    
    @objc func displayadc(){
        choiseUser?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func getTopViewController() -> UIViewController? {
            guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
                  let rootViewController = windowScene.windows.first?.rootViewController else {
                return nil
            }
            
            var topViewController = rootViewController
            while let presentedViewController = topViewController.presentedViewController {
                topViewController = presentedViewController
            }
            
            return topViewController
        }
}
